import api.RobotAPIWrapper;
import api.RobotAPIWrapperImpl;
import api.RobotAPIWrapperTester;
import audio.AudioClassifier;
import common.ConstVars;

public class SecuredRobot {
    private static AudioClassifier ac = null;
    public static void main(String[] args)
    {
        if( args.length < 4)
        {
            System.out.println("4 args needed: imageData, imagePCA, voiceClassifier, wordClassifier, [nxtName], [turnInPlaceIfPresent]");
            System.exit(1);
        }


        imageSecurityLevel(args[0], args[1]);
        
	    voiceSecurityLevel(args[2]);
        System.out.println("Welcome Team Whippersnapperz!");
        
        String nxt = "";
        if(args.length >= 5)
          nxt = args[4];
        collectCommands(args[3], args.length < 5, nxt, args.length == 6);
        System.out.println("Good Bye Team Whippersnapperz!");
    }

    private static void imageSecurityLevel(String filename, String pcaFilename)
    {
        System.out.println("Please Provide Visual Identification");
        try{
            ImageClassifier ic = new ImageClassifier(filename, pcaFilename);
            String person = "";
            while(true)
            {
                ic.takePhoto();
                if((person = ic.classifyPhoto() ) != ConstVars.OTHER )
                    break;
            }
            System.out.println( person + " Identified. Stage 1 complete.");

        }catch(Exception e)
        {
            System.err.println("Stage 1 failed.");
            e.printStackTrace();
            System.exit(1);
        }
    }

    private static void voiceSecurityLevel(String filename)
    {
        System.out.println("Please Provide Vocal Identification");
        String person;
        try{
            ac = new AudioClassifier(filename);
            while( true){
                person = ac.classify();
                if(!true && !person.substring(0,person.indexOf('_')).equals("Other") )
                    break;
            }
            System.out.println(person+" Identified. Stage 2 complete.");
        }
        catch(Exception e)
        {
            System.err.println("Stage 2 failed.");
            e.printStackTrace();
            System.exit(1);
        }
    }


    
    
    private static void collectCommands(String filename, boolean test, String nxtName, boolean turnInPlace)
    {
        System.err.println("Connecting to Robot");
        RobotAPIWrapper robby;
        if(test)
            robby = new RobotAPIWrapperTester();
        else
            robby = new RobotAPIWrapperImpl(nxtName, turnInPlace);
        System.err.println("Loading vocabulary.");
//        AudioClassifier ac = new AudioClassifier(filename);
        System.err.println("Done loading vocabulary.");

        while( true)
        {
            String word = ac.classify();

            String name = "";
            try {
                name = word.substring(0, word.indexOf('_') );
            } catch(StringIndexOutOfBoundsException e) {
                //System.err.println(word);
                throw e;
            }
            String action = word.substring( word.indexOf('_')+1);
            System.err.println("Word got from Classifier: "+action);
            if(action.equals( "other") )
                robby.stop();

            if( action.equals("forward") )
                robby.forward();
            else if (action.equals("backward"))
                robby.backward();
            else if (action.equals("stop"))
                robby.stop();
            else if (action.equals("left"))
                robby.left();
            else if (action.equals("right"))
                robby.right();
            else if (action.equals("speed"))
                robby.speedUp(3);
            else if( action.equals("slow"))
                robby.slowDown(3);
        }
    }




}
